package cn.xiaou.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.xiaou.domain.User;
import cn.xiaou.domain.UserReal;
import cn.xiaou.draw.MyJButtom;
import cn.xiaou.service.UserService;
import cn.xiaou.service.impl.UserServiceImpl;
import cn.xiaou.utils.FontUtils;
import cn.xiaou.utils.ImageManagerUtils;

public class RealnNameFrame extends JFrame implements ActionListener,FocusListener {
	
	private static final long serialVersionUID = 1L;
	private JLabel name,id;
	private JTextField NameText,IdText;
	private JButton Confirm,Close;
	private UserService userService = new UserServiceImpl();
	private User user;
	private JFrame mainFrame;
	private JLabel rename;
	private JLabel recode;
	public RealnNameFrame(User user,JFrame mainFrame)  {
		this.user= user;
		this.mainFrame = mainFrame;
		setSize(459,317);
		setAlwaysOnTop(true);
		setIconImage(getToolkit().getImage(ImageManagerUtils.icoUrl));	
		ImageIcon background = new ImageIcon(ImageManagerUtils.RealNameBg);
		JLabel bg = new JLabel(background);
		bg.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		JPanel panel = (JPanel) getContentPane();  
		 //设置透明
		panel.setOpaque(false);
		 //将背景图的label置于窗体的底层
		getLayeredPane().add(bg, new Integer(Integer.MIN_VALUE));  
		setLocationRelativeTo(null);
		setLayout(null);
		panel.setLayout(null);
		setUndecorated(true);
	    setBackground(new Color(0,0,0,0));
	    
	    Font fontstyle=new Font("黑体", Font.PLAIN, 20);
	    
	    name=new JLabel("真实姓名：");
	    name.setFont(fontstyle);
	    panel.add(name);
		name.setBounds(60, 50, 200, 100);
	    
	    id=new JLabel("身份证号码：");
	    id.setFont(fontstyle);
	    panel.add(id);
	    id.setBounds(40, 100, 200, 100);
	    
	    NameText=new JTextField();
	    NameText.setFont(fontstyle);
	    panel.add(NameText);
	    NameText.setBounds(150, 90, 200, 25);
	    NameText.addFocusListener(this);
	    
	    IdText=new JTextField();
	    IdText.setFont(fontstyle);
	    panel.add(IdText);
	    IdText.setBounds(150, 140, 200, 25);
	    IdText.addFocusListener(this);
	    
//	    ImageIcon confirmP = new ImageIcon(ImageManagerUtils.Confirm);
	    Confirm=new MyJButtom(ImageManagerUtils.qdd);
	    Confirm.setFont(fontstyle);
	    panel.add(Confirm);
	    Confirm.setBounds(180, 200, 100, 50);
	    Confirm.addActionListener(this);
//	    Confirm.setIcon(confirmP);
	    Confirm.setMargin(new Insets(0, 32, 0, 0));  
	    Confirm.setOpaque(false);
	    Confirm.setBackground(new Color(0,0,0,0));
	    

	    
	    
	    
//	    ImageIcon closeP = new ImageIcon(ImageManagerUtils.Close);
	    Close=new MyJButtom(ImageManagerUtils.exit);
	    Close.setFont(fontstyle);
	    panel.add(Close);
	    Close.setBounds(350, 200, 100, 50);
	    Close.addActionListener(this);
//	    Close.setIcon(closeP);
	    Close.setMargin(new Insets(0, 32, 0, 0));  
	    
	    
	    UserReal userReal = userService.checkRealnameExist(user);
	    if(userReal!=null) {
	    	rename = new JLabel(userReal.getRealname());
	    	rename.setFont(FontUtils.font1);
	    	rename.setBounds(140, 90, 200, 25);
	    	panel.add(rename);
	    	recode = new JLabel(userReal.getId_card());
	    	recode.setFont(FontUtils.font1);
	    	recode.setBounds(140, 140, 200, 25);
	    	panel.add(recode);
	    	IdText.setVisible(false);
	    	NameText.setVisible(false);
	    	Confirm.setVisible(false);
	    }
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == Close) {
			setVisible(false);
			mainFrame.setEnabled(true);
		}	
		if(e.getSource() == Confirm) {
			this.setAlwaysOnTop(false);
			if(IdText.getText().equals("身份证未输入") || IdText.getText().equals("身份证输入身份有误") || NameText.getText().equals("姓名未输入") || NameText.getText().equals("") || IdText.getText().equals("")) {
				Object[] options = { "确定"}; 
				JOptionPane.showOptionDialog(null, "身份信息未输入", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
						
			}else {
				UserReal userReal = new UserReal();
				userReal.setRealname(NameText.getText());
				userReal.setId_card(IdText.getText());
				userReal.setUser(user);
				Boolean state = userService.addRealName(userReal);
				if(state) {
					Object[] options = {"返回"}; 
					
					JOptionPane.showOptionDialog(null, "实名认证成功", "提示", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
							null, options,options[0]); 
					
					
					mainFrame.setEnabled(true);
					dispose();
					
				}
			}
			
		}
	}
	@Override
	public void focusGained(FocusEvent e) {
		((JTextField)e.getSource()).setText("");
		
	}
	@Override
	public void focusLost(FocusEvent e) {
		
		if(e.getSource() == IdText) {
			if(IdText.getText().equals("")) {
				IdText.setText("身份证未输入");
				return;
			}
			if(!IdText.getText().matches("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$")) {
				IdText.setText("身份证输入身份有误");
				return;
			}
		}
		if(e.getSource() == NameText) {
			if(NameText.getText().equals("")) {
				NameText.setText("姓名未输入");
				return;
			}
		}
		
	}
	
	

    
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
